package cn.niit.letuseatdemo.controller.user;

import cn.niit.letuseatdemo.common.enums.ExceptionCodeEnum;
import cn.niit.letuseatdemo.entity.domain.Store;
import cn.niit.letuseatdemo.entity.domain.User;
import cn.niit.letuseatdemo.entity.domain.UserAddress;
import cn.niit.letuseatdemo.entity.vo.response.BaseResponse;
import cn.niit.letuseatdemo.service.UserAddressService;
import cn.niit.letuseatdemo.service.UserService;
import cn.niit.letuseatdemo.util.H5LoginUtil;
import cn.niit.letuseatdemo.util.RedisUtil;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 收货地址管理
 * @Author Zhang Shiqi
 * @CreateTime 2022/10/31 0:06
 */
@Slf4j
@RestController
@RequestMapping("/api/address")
public class UserAddressController {

    @Autowired
    private UserAddressService userAddressService;


    @Autowired
    private RedisUtil redisUtil;



   /*
   * @Description: 查默认地址
   * @Author: Zhang Shiqi
   * @CreateTime： 2023/4/24 2:09
    * @return: cn.niit.letuseatdemo.entity.vo.response.BaseResponse
   **/
   @GetMapping("/defualt")
   public BaseResponse addressMes(){
       User user = H5LoginUtil.getLoginUser();//通过公共的用户登录工具类获取登录用户信息
       Long userId= user.getUserId();
       UserAddress userAddress =userAddressService.selectUserDefualtAddress(userId);
       System.out.println("默认地址："+userAddress);
       return BaseResponse.success(userAddress);
   }

   /*
   * @Description: 新增地址
   * @Author: Zhang Shiqi
   * @CreateTime： 2023/4/24 23:40
    * @param: userAddress 
    * @return: cn.niit.letuseatdemo.entity.vo.response.BaseResponse 
   **/
   @PostMapping("/addAddress")
   public  BaseResponse addressAdd(@RequestBody UserAddress userAddress){
       int rows = userAddressService.insertUserAddress(userAddress);
       if(rows == 1){
           return BaseResponse.success();
       }
       return BaseResponse.error(ExceptionCodeEnum.COMMON_ADD_ERROR);
   }

    /*
    * @Description: 根据地址id查地址
    * @Author: Zhang Shiqi
    * @CreateTime： 2023/4/25 4:13
     * @param: addressId
     * @return: cn.niit.letuseatdemo.entity.vo.response.BaseResponse
    **/
    @GetMapping("/{addressId}")
    public BaseResponse addressMesByAd(@PathVariable("addressId")Long addressId){

        UserAddress userAddress =userAddressService.selectUserAddressByAddressId(addressId);

        return BaseResponse.success(userAddress);
    }

    @PutMapping("/edit")
    public BaseResponse editSave(@RequestBody UserAddress userAddress){
        int rows = userAddressService.updateUserAddress(userAddress);
        if(rows == 1){
            return BaseResponse.success();
        }
        return BaseResponse.error(ExceptionCodeEnum.COMMON_ADD_ERROR);
    }

    /*
     * @Description: 根据地址id删除配送地址信息
     * @Author: Zhang Shiqi
     * @CreateTime： 2022/12/8 19:53
     * @param: id
     * @return: java.lang.String
     **/
    @DeleteMapping("/delete/{addressId}")
    @ResponseBody
    public BaseResponse remove(@PathVariable("addressId") Long addressId){
        int rows = userAddressService.deleteUserAddressByAddressId(addressId);
        if(rows == 1){
            return BaseResponse.success();
        }else{
            return BaseResponse.error(ExceptionCodeEnum.COMMON_ADD_ERROR);
        }
    }

    /*
   * @Description: 显示送货地址页面
   * @Author: Zhang Shiqi
   * @CreateTime： 2022/10/31 1:33
    * @param: model
    * @param: userAddress
    * @return: java.lang.String
   **/
//   @GetMapping
////    public String index(Model model, UserAddress userAddress){
////        model.addAttribute("userAddress",userAddress);
////        List<UserAddress> userAddressList=userAddressService.selectUserAddressList(userAddress);
////        PageInfo<UserAddress> pageInfo = new PageInfo<>(userAddressList);
////        log.info("分页对象 ={}",pageInfo);
////        log.info("查询出来的地址列表：{}"+userAddressList);
////        model.addAttribute("userAddressList",userAddressList);
////        model.addAttribute("pageInfo",pageInfo);
////        return "user/address";
////    }

    /*
    * @Description: 接收用户请求，打开添加、修改页面
    * @Author: Zhang Shiqi
    * @CreateTime： 2022/10/31 1:34
     * @param: model
     * @param: addressId
     * @return: java.lang.String
    **/
//    @GetMapping("/{addressId}")
//    public String saveIndex(Model model,@PathVariable("addressId") Long addressId){
//        log.info("要修改的id："+addressId);
//        if (null != addressId && addressId >0){
//            UserAddress userAddress = userAddressService.selectUserAddressByDId(addressId);
//            model.addAttribute("userAddress",userAddress);
//        }
//        return "user/addressadd";
//    }


    /*
    * @Description: 保存
    * @Author: Zhang Shiqi
    * @CreateTime： 2022/10/31 12:11
     * @param: userAddress
     * @param: session
     * @return: java.lang.String
    **/
//    @PostMapping
//    public String addSave(UserAddress userAddress, HttpSession session){
//        int rows =userAddressService.insertUserAddress(userAddress);
//        if (rows <= 0){
//            return "user/addressadd";
//        }
//        //如果插入成功，则重定向至列表页面
//        return "redirect:/user/address";
//    }


    /*
    * @Description: 根据地址id修改配送地址信息
    * @Author: Zhang Shiqi
    * @CreateTime： 2022/11/1 13:12
     * @param: userAddress
     * @return: java.lang.String
    **/
//    @PutMapping
//    public String editSave(UserAddress userAddress){
//        int rows = userAddressService.updateUserAddress(userAddress);
//        if (rows <= 0){
//            return "user/address";
//        }
//        //插入成功
//        return "redirect:/user/address";
//    }




}
